package com.fiveGroup.util;


import com.fiveGroup.pojo.User;

public class UserContextHolder {
    //定义一个容器 用于存储用户上下文信息，方便全局获取当前线程用户信息
    private static final ThreadLocal<User> context = new ThreadLocal<>();

    //获取用户Id
    public static Long getUserId() {
        System.out.println("getUserId: " + context.get().getId());
        return context.get().getId();
    }

    //获取用户名称
    public static String getUserName() {
        return context.get().getUsername();
    }

    //获取当前用户
    public static User getUser() {
        System.out.println("getUser: " + context.get());
        return context.get();
    }

    //设置当前线程用户
    public static void setUser(User user) {
        System.out.println("setUser: " + user);
        context.set(user);
    }

    //清除当前线程用户信息方法，防止内存泄露
    public static void clear() {
        context.remove();
    }
}
